<template>
  <div v-loading="loading">
    <el-form
      :model="detail"
      label-width="80px"
      ref="formRef"
      :rules="rules"
      style="max-width: 500px"
    >
      <el-form-item prop="CourseName" label="课程">
        <el-input v-model="detail.CourseName" clearable />
      </el-form-item>
      <el-form-item prop="Teacher" label="任教">
        <el-select v-model="detail.Teacher" clearable filterable placeholder="Select">
          <el-option
            v-for="item in allUser"
            :key="item.Pk"
            :label="item.UserName"
            :value="item.Pk"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="Optional" label="选修">
        <el-switch v-model="detail.Optional" active-value="Y" inactive-value="N" />
      </el-form-item>
            <el-form-item prop="GroupClass" label="大课">
          <el-switch v-model="detail.GroupClass" active-value="Y" inactive-value="N" />
        </el-form-item>
      <el-form-item prop="Color" label="辨别色">
        <el-color-picker v-model="detail.Color" />
      </el-form-item>
    </el-form>
    <div v-loading="loading" class="my_opt_btn_pannel">
      <div style="text-align: left">
        <el-button @click="router.back()">返回</el-button>
      </div>
      <div style="text-align: right; padding-right: 20px">
        <el-button v-if="optUser.isAdm" type="primary" @click="submit">保存</el-button>
        <el-button v-if="optUser.isAdm" @click="setInfo()">重置</el-button>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { getCourse, saveCourse } from '@/api/course'
import { useUserStore } from '@/stores/user'
import type { CourseModel } from '@/type/Course'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { listUser } from '@/api/user'
import type { UserModel } from '@/type/User'
import { DeconstructionKvModel } from '@/utils/objUtils'
const router = useRouter()
const route = useRoute()
const loading = ref(false)
const detail = ref<CourseModel>({})
const optUser = useUserStore()
const allUser = ref<UserModel[]>([])
let detail_copy = ''
onMounted(async () => {
  if (route.query.pk) {
    loading.value = true
    try {
      const c = (await getCourse(route.query.pk.toString())).Data[0]!
      DeconstructionKvModel(c)
      detail.value = c
    } finally {
      loading.value = false
    }
  } else {
    detail.value.Color = randomHexColor()
  }
  allUser.value = (await listUser({ pageSize: 10000 })).Data
  detail_copy = JSON.stringify(detail.value)
})

const setInfo = () => {
  detail.value = JSON.parse(detail_copy)
}
const formRef = ref<FormInstance>()

const rules = reactive<FormRules>({
  CourseName: [{ required: true, message: '请输入课程名', trigger: 'blur' }],
  Teacher: [{ required: true, message: '请选择任课老师', trigger: 'blur' }],
})
const randomHexColor = () => {
  return (
    '#' +
    Math.floor(Math.random() * 0xffffff)
      .toString(16)
      .padStart(6, '0')
  )
}

const submit = async () => {
  loading.value = true
  try {
    await formRef.value?.validate()
    const r = await saveCourse(detail.value)
    const rd = r.Data[0]!
    DeconstructionKvModel(rd)
    detail_copy = JSON.stringify(rd)
    detail.value = rd
    ElMessage.success('操作成功')
  } finally {
    loading.value = false
  }
}
</script>
